//
//  YHMachineServiceChargeViewController.m
//  YHApp
//
//  Created by -- on 2021/7/24.
//

#import "HAMachineServiceChargeVC.h"
#import "HAMachineServiceChargeMakeCell.h"
#import "HAMachineServiceChargeMo.h"

@interface HAMachineServiceChargeVC ()<UITableViewDataSource,UITableViewDelegate,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic ,strong) NSMutableArray *dataArray;
/***/
@property (nonatomic, strong) HAMachineServiceChargeMo *model;


@end

@implementation HAMachineServiceChargeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.baseTLabel.text = @"服务费";
    [self loadData];
    [self initUI];

    // Do any additional setup after loading the view.
}

- (void)initUI{
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(app_topH+44);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}

-(void)loadData {
    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodGET;
    request.requestUrl = @"customer/service.html";
    request.parms = @{
        @"sn":self.sn?:@"",
    };
    [MBProgressHUD showLoading:self.view];
    
    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {
            [MBProgressHUD hideHUDForView:self.view];
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                self.model = [HAMachineServiceChargeMo mj_objectWithKeyValues:dataModel.data];
                if (self.model.rates.count > 0) {
                    [self.dataArray removeAllObjects];
                    [self.dataArray addObject:@"内容"];
                    if (self.model.disabled == NO) {
                        [self.dataArray addObject:@"提交"];
                    }
                    [self.tableView reloadData];
                }

            }else {
                [MBProgressHUD showError:dataModel.msg];
            }
        }
    }];

}

-(void)mitButtonClick {
    
    [self.view endEditing:YES];

    NSMutableArray *mitAateArr = [NSMutableArray array];
    for (int i = 0; i < self.model.rates.count; i ++) {
        YHMachineServiceChargeRatesModel *rateMo = self.model.rates[i];
        [mitAateArr addObject:@{
            @"value":rateMo.value?:@"",
            @"field":rateMo.code?:@"",
        }];
    }

    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodPOST;
    request.requestUrl = [NSString stringWithFormat:@"reward/service.html"];
    request.parms = @{
        @"sn":self.model.sn,
        @"item":[mitAateArr mj_JSONString],
    };
    [MBProgressHUD showLoading:self.view];

    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {
            [MBProgressHUD hideHUDForView:self.view];
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                [MBProgressHUD showSuccess:@"提交成功"];
                [self.navigationController popViewControllerAnimated:YES];
            }else {
                [MBProgressHUD showError:dataModel.msg];
            }
        }
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];
    if ([title isEqualToString:@"内容"]) {
        CGFloat h = 0;
        if (self.model.customer) {
            h += 44+54+54;
        }
        if (![NSString isEmptyString:self.model.policy]) {
            h += 10 + 54;
        }
        h += 10;
        h += (54+28+10) * self.model.rates.count;
        return h;
    }
    if ([title isEqualToString:@"提交"]) {
        return 54+60;
    }
    return 0;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];
    if ([title isEqualToString:@"内容"]) {
        HAMachineServiceChargeMakeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YHMachineServiceChargeMakeCellID"];
        if (!cell) {
            cell = [[HAMachineServiceChargeMakeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YHMachineServiceChargeMakeCellID"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = self.model;
        return cell;
    }
    if ([title isEqualToString:@"提交"]) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YHMachineRateMakeMitCellID"];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YHMachineRateMakeMitCellID"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setCellMit:cell.contentView];
        return cell;
    }
    return [UITableViewCell new];
}

-(void)setCellMit:(UIView *)contentView {
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
    contentView.backgroundColor = [UIColor khaBGColor];
    UIButton *mitButton = [[UIButton alloc]init];
    [mitButton setTitle:@"提交" forState:UIControlStateNormal];
    [mitButton setTitleColor:[UIColor colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal];
    mitButton.titleLabel.font = [UIFont systemFontOfSize:20];
    mitButton.backgroundColor = [UIColor colorWithHexString:@"#19191A"];
    [mitButton addTarget:self action:@selector(mitButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:mitButton];
    [mitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(25);
        make.right.mas_equalTo(-25);
        make.centerY.mas_equalTo(0);
        make.height.mas_offset(54);
    }];
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        // 去掉滚动条
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.emptyDataSetSource = self;
        _tableView.emptyDataSetDelegate = self;

        // 设置背景颜色
        _tableView.backgroundColor = [UIColor khaBGColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

#pragma mark - 无数据处理 DZNEmptyDataSetSource DZNEmptyDataSetDelegate
//设置title
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = @"暂无数据~";
    NSDictionary *attributes = @{
        NSFontAttributeName:[UIFont systemFontOfSize:14],
        NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#999999"]
    };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}

//设置image
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIImage imageNamed:@"hl_nodata"];
}


@end
